package com.rfid.emergencywarehouse.util;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.rfid.emergencywarehouse.R;

public class DialogUtil {

    public static void showSuccessDialog(Context context,View parent,String msg) {
//        AlertDialog dialog = new AlertDialog.Builder(context)
//                .setView(R.layout.success_dialog_view)
//                .setCancelable(false)
//                .show();
//        dialog.setCanceledOnTouchOutside(false);
        if (context == null) {
            return;
        }
        View popupView = LayoutInflater.from(context).inflate(R.layout.success_dialog_view, null);
        TextView textView = popupView.findViewById(R.id.msg);
        textView.setText(msg);
        PopupWindow popupWindow = new PopupWindow(popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置背景为透明
        popupWindow.setOutsideTouchable(true); // 设置外部可触摸
        popupWindow.setFocusable(true); // 设置获取焦点
        popupWindow.showAtLocation(parent, Gravity.CENTER,0,0);
        textView.postDelayed(new Runnable() {
            @Override
            public void run() {
                popupWindow.dismiss();
            }
        },3000);
    }

    public static void showTipDialog(Context context,View parent,String msg) {
        if (context == null) {
            return;
        }
        View popupView = LayoutInflater.from(context).inflate(R.layout.dialog_tip, null);
        TextView textView = popupView.findViewById(R.id.msg);
        textView.setText(msg);
        PopupWindow popupWindow = new PopupWindow(popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置背景为透明
        popupWindow.setOutsideTouchable(false); // 设置外部可触摸
        popupWindow.setFocusable(false); // 设置获取焦点
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,60);
        popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                System.out.println("View");
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return false;
                }
                return false;
            }
        });
        textView.postDelayed(new Runnable() {
            @Override
            public void run() {
                popupWindow.dismiss();
            }
        },4000);
    }
}
